package com.suzhe.ymall.product.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.suzhe.ymall.common.annotation.LoginAuth;
import com.suzhe.ymall.common.pojo.ApiResEntity;
import com.suzhe.ymall.product.pojo.Category;
import com.suzhe.ymall.product.service.ICategoryService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 商品类目表 前端控制器
 * </p>
 *
 * @author suzhe
 * @since 2019-07-04
 */
@RestController
@RequestMapping("/product/category")
@Slf4j
public class CategoryController {

    @Autowired
    ICategoryService iCategoryService;


    @LoginAuth("productCategoryView")
    @GetMapping("/tree")
    public ApiResEntity tree() {
        List<Category> categories = iCategoryService.list(new QueryWrapper<Category>() //
                .orderByAsc("order_num"));
        List<Category> parentlist = new ArrayList<>();
        categories.forEach(item -> {
            if (item.getParentId() == 0){
                parentlist.add(item);
            }
        });
        parentlist.forEach(item -> {
            setChildren(item,categories);
        });
        return ApiResEntity.ok(parentlist);
    }


    @LoginAuth("productCategoryView")
    @GetMapping("/list")
    public ApiResEntity list(){
        List<Category> categories = iCategoryService.list(new QueryWrapper<Category>() //
                .orderByAsc("order_num"));
        return ApiResEntity.ok(categories);
    }
    /**
     * 递归设置子节点
     * @param category
     * @param categorys
     */
    public void setChildren(Category category,List<Category> categorys){
        List<Category> children = getChildren(category.getId(), categorys);
        if (!children.isEmpty()){
            category.setChildren(children);
            children.forEach(item -> {
                setChildren(item,categorys);
            });
        }
    }

    /**
     * 获取多有的孩子节点
     * @param parentId
     * @param categorys
     * @return
     */
    public List<Category> getChildren(Integer parentId,List<Category> categorys){
        List<Category> children = new ArrayList<>();
        categorys.forEach(item -> {
            if (parentId == item.getParentId()){
                children.add(item);
            }
        });
        return children;
    }

    @PostMapping("/save")
    @LoginAuth("productCategorySave")
    @ApiOperation(value="保存类目")
    public ApiResEntity save(@RequestBody Category category){
        log.debug("save category:{}",category);

        iCategoryService.save(category);
        return ApiResEntity.ok();
    }

    @ApiOperation(value="更新类目")
    @PutMapping("/update")
    @LoginAuth("productCategoryUpdate")
    public ApiResEntity update(@RequestBody Category category){
        log.debug("update category:{}",category);
        Category existCategory = iCategoryService.getById(category.getId());
        if (existCategory == null){
            return ApiResEntity.error("不存在该类目");
        }
        category.setUpdateTime(new Date());
        iCategoryService.updateById(category);
        return ApiResEntity.ok();
    }


    @ApiOperation(value="删除类目")
    @DeleteMapping("/delete")
    @LoginAuth("productCategoryDelete")
    public ApiResEntity delete(@RequestBody Category category){
        log.debug("delete category:{}",category);
        iCategoryService.removeById(category.getId());
        return ApiResEntity.ok();
    }

}
